package com.akk.module.hkpay.util;

import java.io.*;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.commons.net.ftp.FTPSClient;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * FTP例子
 */
@Service
public class FtpUtil {

    @Value("${hk.ftp.ip}")
    private String host;
    @Value("${hk.ftp.port}")
    private Integer port;
    @Value("${hk.ftp.username}")
    private String username;
    @Value("${hk.ftp.password}")
    private String password;
    public static String serverPath = "/test/";

    public static void main(String[] args) {
        File file = new File("G:\\1.png");
        try {
            boolean flag = new FtpUtil().upload("113.108.195.242", 47022, "99999999", "123456", "file.png", new FileInputStream(file));
            System.out.println(flag);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
//		System.out.println(flag);
    }

    /**
     * 商户入网上传文件
     */
    public boolean uploadFile(MultipartFile file, String fileName) {
//        File file1 = new File("D:/"+fileName);
        try {
//            if (!file1.exists())
//                file1.createNewFile();
//            FileOutputStream os = new FileOutputStream(file1);
//            os.write(file.getBytes());
            return upload(host, port, username, password, fileName, file.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean upload(String host, int port, String username, String password, String basePath, String filePath, String filename, InputStream is) {
        boolean result = com.akk.module.upload.utils.FtpUtil.uploadFile(host, port, username, password, basePath, filePath, filename, is);
        return result;
    }

    /**
     * 上传文件
     *
     * @param host     地址
     * @param port     端口
     * @param username 用户名
     * @param password 密钥
     * @param filename 文件名
     * @param fileIs   文件输入流
     * @return
     */
    public boolean upload(String host, int port, String username, String password, String filename, InputStream fileIs) {
        boolean result = false;
        FTPSClient ftpClient = null;
        InputStream is = null;
        try {
            ftpClient = new FTPSClient();
            ftpClient.setControlEncoding("UTF-8");
            ftpClient.connect(host, port);
            ftpClient.login(username, password);
            int replyCode = ftpClient.getReplyCode();
            if (!FTPReply.isPositiveCompletion(replyCode)) {
                return false;
            }
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
            ftpClient.execPROT("P");
            ftpClient.enterLocalPassiveMode();
            is = fileIs;
            result = ftpClient.storeFile(filename, is);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != ftpClient) {
                try {
                    ftpClient.logout();
                    ftpClient.disconnect();
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    ftpClient = null;
                }
            }
            if (null != is) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    is = null;
                }
            }
        }
        return result;
    }

}
